/**
 * 视野缓存系统
 * 
 * 功能：
 * 1. 缓存视野计算结果
 * 2. 管理缓存更新
 * 3. 优化性能
 * 4. 提供快速查询
 */
export class VisionCache {
    private static readonly CACHE_TIME = 0.1; // 缓存时间
    private cache: Map<string, {
        result: boolean,
        timestamp: number
    }> = new Map();

    public getCachedResult(from: Vec3, to: Vec3): boolean | null {
        const key = this.getCacheKey(from, to);
        const cached = this.cache.get(key);
        
        if (!cached) return null;
        
        if (Date.now() - cached.timestamp > VisionCache.CACHE_TIME * 1000) {
            this.cache.delete(key);
            return null;
        }

        return cached.result;
    }

    public cacheResult(from: Vec3, to: Vec3, result: boolean): void {
        const key = this.getCacheKey(from, to);
        this.cache.set(key, {
            result,
            timestamp: Date.now()
        });
    }

    private getCacheKey(from: Vec3, to: Vec3): string {
        // 四舍五入到0.1的精度以增加缓存命中率
        const fx = Math.round(from.x * 10) / 10;
        const fz = Math.round(from.z * 10) / 10;
        const tx = Math.round(to.x * 10) / 10;
        const tz = Math.round(to.z * 10) / 10;
        return `${fx},${fz}-${tx},${tz}`;
    }

    public clear(): void {
        this.cache.clear();
    }
} 